//
//  CHComplainListFrame.m
//  CarFile
//
//  Created by 陈浩 on 2017/6/6.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHComplainListFrame.h"
#define MaxImageCount 6 //最大的图片个数
#define imgViewW 100
// 图片的间距
#define imgMargin 10
#define imgViewH imgViewW
#define TitleFont [UIFont systemFontOfSize:14]
@implementation CHComplainListFrame
-(NSMutableArray *)picFrms{
    if (!_picFrms) {
        _picFrms = [NSMutableArray array];
    }
    return _picFrms;
    
}


-(void)setModel:(CHComplainListModel *)model{
    _model = model;
     CGFloat padding = 10;

    _titleFrm = CGRectMake(padding, padding, UIScreenW /2 - padding, 30);
    _timeFrm = CGRectMake(UIScreenW /2, padding, UIScreenW/2 -padding, 30);
    _iconFrm = CGRectMake(padding, CGRectGetMaxY(_timeFrm) +5, 30, 30);
    _phoneFrm = CGRectMake(CGRectGetMaxX(_iconFrm) + padding, CGRectGetMaxY(_titleFrm)+5, 150, 30);
    _dbFrm = CGRectMake(UIScreenW - 40 - padding, CGRectGetMaxY(_timeFrm) +padding, 40, 25);
    _wxFrm = CGRectMake(UIScreenW - 40- 60 - 20, CGRectGetMaxY(_timeFrm) + padding, 60, 25);
    
    
    CGSize maxSize = CGSizeMake(UIScreenW -2*padding, MAXFLOAT);
    NSDictionary *attr = @{NSFontAttributeName:TitleFont};
    CGSize evaluatedetailsSize = [self boundingRectWithSize:maxSize attributes:attr text:model.evaluatedetails];
    _evaluatedetailsFrm = CGRectMake(padding, CGRectGetMaxY(_iconFrm)+padding, evaluatedetailsSize.width, evaluatedetailsSize.height);
    
    if (![model.complainaccordimg isEqualToString:@""]) {
        
        NSArray  *pics = [model.complainaccordimg componentsSeparatedByString:@","];
      
        CGSize photoViewSize = [self sizeWithPhotosCount:pics.count];
        _photosViewFrm = CGRectMake(padding, CGRectGetMaxY(_evaluatedetailsFrm) + padding, UIScreenW - 2*padding, photoViewSize.height);
        
        NSDictionary *attr1 = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
        CGSize supervisevalueSize = [self boundingRectWithSize:maxSize attributes:attr1 text:[NSString stringWithFormat:@"督办意见：%@",model.supervisevalue]];
        
        _bgViewFrm = CGRectMake(0, CGRectGetMaxY(_photosViewFrm)+padding, UIScreenW, supervisevalueSize.height + 2*padding);
        
        _supervisevalueFrm = CGRectMake(padding, padding, supervisevalueSize.width, supervisevalueSize.height);
        
    }else{
        
        NSDictionary *attr1 = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
        CGSize valuatevalueSize = [self boundingRectWithSize:maxSize attributes:attr1 text:[NSString stringWithFormat:@"督办意见：%@",model.supervisevalue]];
        
        _bgViewFrm = CGRectMake(0, CGRectGetMaxY(_evaluatedetailsFrm)+padding, UIScreenW, valuatevalueSize.height + 2*padding);
        
        _supervisevalueFrm = CGRectMake(padding, padding, valuatevalueSize.width, valuatevalueSize.height);
        
    }
    


    _cellHeight = CGRectGetMaxY(_bgViewFrm);
    
}




-(CGSize)sizeWithPhotosCount:(NSInteger)count{

    // 如果没有图片，图片size就是0
    if (count == 0) {
        return CGSizeZero;
    }
    

    /*
     * 如果有4张图片，显示2列，其它情况显示3列
     */
    NSInteger totalRow = count > 3 ? 2 : 1;
    

    
    
    // photosView的宽高
    // photosView的宽高
    CGFloat photosViewW = UIScreenW - 20;
    CGFloat photosViewH = totalRow * imgViewH + (totalRow - 1) * imgMargin;
    
    

    NSLog(@"图片view的宽高%f-- %f",photosViewW,photosViewH);
    return CGSizeMake(photosViewW, photosViewH);
    
}



- (CGSize)boundingRectWithSize:(CGSize)maxSize attributes:(NSDictionary *)attr text:(NSString *)text
{
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
}


@end
